/*
 *所有关于tb_project_sourceinfo类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_sourceinfoService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Entity.DomainModels;
using System.Linq;
using MYData.Core.Utilities;
using MYData.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using System.Threading.Tasks;
using MYData.Core.EFDbContext;
using System.Collections.Generic;
using MYData.Entity.DTO.ApiEntity.Input;
using NuGet.Packaging;
using Mapster;

namespace MYData.System.Services
{
    public partial class tb_project_sourceinfoService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_sourceinfoRepository _repository;//访问数据库
        private readonly VOLContext _context;
        [ActivatorUtilitiesConstructor]
        public tb_project_sourceinfoService(
            Itb_project_sourceinfoRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            VOLContext context
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _context = context;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        WebResponseContent webResponse = new WebResponseContent();

        /// <summary>
        /// 获取项目来源信息
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> GetSourceListAsync(string dicCode)
        {
            if (dicCode == "XMLY")
            {
                var data = await _repository.FindAsIQueryable(x => x.dicCode == "XMLY").ToListAsync();
                return webResponse.OK("查询成功", data);
            }
            else
            {
                var data = await _repository.FindAsIQueryable(x => x.dicCode == dicCode).ToListAsync();
                return webResponse.OK("查询成功", data);
            }
        }
        /// <summary>
        /// 获取项目来源
        /// MJZ 2023/3/29/15:31
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> GetSourceInforAsync()
        {
            List<ProjectSourceOutputDto> projectSourceOutputDto = GetChildren("XMLY");
            return webResponse.OK("项目来源获取成功", projectSourceOutputDto);
        }
        /// <summary>
        /// 套娃
        /// MJZ 2023/3/29/15:31
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<ProjectSourceOutputDto> GetChildren(string id)
        {
            List<ProjectSourceOutputDto> projectSourceOutputDto = new List<ProjectSourceOutputDto>();
            //获取模板科目表信息
            var SourceList = _context.Set<tb_project_sourceinfo>().Where(x => x.dicCode.Equals(id)).ToList();
            foreach (var item in SourceList)
            {
                if (GetChildren(item.Id).Count() > 0)
                {
                    projectSourceOutputDto.Add(new ProjectSourceOutputDto
                    {
                        productId = item.productId,
                        dicCode = item.dicCode,
                        dicKey = item.dicKey,
                        dicSort = item.dicSort,
                        dicStatus = item.dicStatus,
                        dicValue = item.dicValue,
                        Id = item.Id,
                        orgId = item.orgId,
                        orgName = item.orgName,
                        productName = item.productName,
                        children = GetChildren(item.Id),
                    });
                }
                else
                {
                    projectSourceOutputDto.Add(item.Adapt<ProjectSourceOutputDto>());
                }


            };
            return projectSourceOutputDto;
        }
    }
}
